const CONSTANTS = {
  EFFECT: 1000
}

export default {
  setStor(key, value) {
    wx.setStorageSync(key, value)
  },
  setFunStor(key, value,callback) {
    wx.setStorageSync(key, value);
    callback();
  },
  getStor(key) {
    return wx.getStorageSync(key)
  },
  remStor(key) {
    return wx.removeStorageSync(key)
  },
  formatFloat(f, digit) { //高精度计算
    let m = Math.pow(10, digit);
    return parseInt(f * m, 10) / m;
  },
  parseFloats(f, digit) {
    return parseFloat(this.formatFloat(f, digit).toFixed(2))
  },
  timeout(callback, time) { //倒计时
    setTimeout(callback, time || CONSTANTS.EFFECT);
  },
  getTimeStamp() {
    let timestamp = Date.parse(new Date());
    return timestamp / 1000;
  },
  clearFormId(arr) {
    // 设置20条
    arr.length >= 20 ? arr.shift() : '';
  },
  setFormId(id) { //设置formID进入Storage
    let arr = [];
    if (Boolean(this.getStor('formId'))) arr = this.getStor('formId');
    //定数清理
    this.clearFormId(arr);
    arr.push({
      form_id: id,
      time: this.getTimeStamp()
    })
    this.setStor('formId', arr)
  },
  onShareAppMessage(res) {//转发函数
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '领沛生鲜',
      path: `/pages/index/index`,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  getLocalTime(date) { //时间戳转成时间
    let now = new Date(date);
    console.log(now)
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  },
  toDateString(timestamp) {
    if (timestamp == undefined || timestamp == 0) return '';
    var date = new Date(timestamp * 1000);    //根据时间戳生成的时间对象
    var Y = date.getFullYear() + '/';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    return Y + M + D + h + m;
  },
}